package cl.fbd.widget_examples.web.snippet

import scala.xml.{NodeSeq, Node, PCData, Text, Unparsed}

import net.liftweb.widgets.trees.model._
import net.liftweb.widgets.trees.treetable._
import cl.fbd.widget_examples.web.model.{TimeSerieMeasureModel, TimeSerie, Measure, Region, Country}
// import net.liftweb.http.{LiftResponse, XhtmlResponse}
import net.liftweb.http.XhtmlResponse
import net.liftweb.http.js.JsCommands
import net.liftweb.http.js.JsCmds._
import net.liftweb.util.Helpers._
import net.liftweb.http.S._
import net.liftweb.http.LiftRules


/*
 * render a treetable of measure and time series
 */

class TreeTableAlone {
  
  def renderMeasure (xhtml: NodeSeq, measure : Measure) = {
    bind ("measure", xhtml, "mm" --> measure.value, 
                             "date" --> Constants.sdf.format (measure.when))
  }
  
  def renderTimeSerie (xhtml: NodeSeq, timeserie : TimeSerie) = {
    bind ("timeserie", xhtml, "name" --> (timeserie.what + " / " + timeserie.where),  
                             "sum" --> timeserie.sum)
  }
  
  def renderRegion (xhtml: NodeSeq, region : Region) = {
    bind ("region", xhtml, "name" --> region.name)
  }
  
  def renderCountry (xhtml: NodeSeq, country : Country) = {
    xhtml
  }
  
  /*
  def renderAjax () : NodeSeq = {
    val treeFunc : () => JsCommands = {
     case _ => 
         val row = JE.JqId ("res")
         JsCommands (JqId >> JsAppend ("res", <div>seteado con AJAX</div>) :: Nil)  
     }
     
     val key = mapFunc(NFuncHolder(treeFunc))
     
     val url = encodeURL(contextPath +
       "/"+LiftRules.ajaxPath)+"?"+key+"=_"
       
     val js = """
        /* <![CDATA[ */		      
        jQuery(function(){
          jQuery("#link").click (function () {
             alert ("ajax call") ;
          """ +
            "jQuery.ajax ( {url: '" + encodeURL(contextPath + "/" + LiftRules.ajaxPath) + "', " + 
                          "timeout: 10000,  type: 'POST', cache: false, data: '" + key + "', dataType: 'script'});" +
          """               
          }) ;
        });
        /* ]]> */    
     """
       
     <head>  
       <script type="text/javascript" charset="utf-8">
       {Unparsed (js)}
       </script>
     </head>
     <a id="link">presionar aqui</a>
  }
  */
  
  class TimeSerieFinder (chile : TreeModel [Country, Region]) {
    def find (id : String) : TreeModel [AnyRef, AnyRef] = {
      println ("buscando valores timeserie: " + id) 
      val idRegion = id.substring (0, id.length -2)
      println ("idRegion :" + idRegion)
      
      val tmRegion = chile.getLeafs.filter (tmts => tmts.getNode.id.toString == idRegion).head 
      val ts = tmRegion.getLeafs.filter (tmts => tmts.getNode.asInstanceOf [TimeSerie].id.toString == id).head
      
      ts
    }
  }
  
  def render (xhtml: NodeSeq) = {
    val model = TimeSerieMeasureModel.initModel
    
    val finderTimeSerie = new TimeSerieFinder (model).find _
    
    val ttMeasure = new TtAjaxHelpers [Measure] (
        renderMeasure _,
        (measure : Measure) => measure.when.getTime.toString,
        (sz : String) => new TreeModel [String, String] ("", Nil)
    )

    val ttTimeSerie = new TtAjaxHelpers [TimeSerie] (
        renderTimeSerie _,
        (timeSerie : TimeSerie) => timeSerie.id.toString,
        finderTimeSerie
    )
      
    val ttRegion = new TtAjaxHelpers [Region] (
        renderRegion _,
        (region : Region) => region.getId,
        (sz : String) => new TreeModel [String, String] ("", Nil)
      )
      
    val ttCountry = new TtAjaxHelpers [Country] (
        renderCountry _,
        (country : Country) => country.getId,
        (sz : String) => new TreeModel [String, String] ("", Nil)
      )
      
    bind ("treetable", xhtml, 
      "render" --> 
        net.liftweb.widgets.trees.treetable.TreeTable.renderAjax (
          model, 
          new net.liftweb.widgets.trees.treetable.TreeTableOptions,
               ttTimeSerie, ttMeasure, ttRegion, ttCountry)
       // "prueba_ajax" --> renderAjax
    )
  }
}

